package xy001.imageloader_weichat;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SpinnerAdapter;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by xiangyang on 15/11/23.
 */
public class ImageAdapter extends BaseAdapter {

    private static Set<String> mSelectImag = new HashSet<String>(); //记录图片是否被选择
    private String mDirPath;
    private List<String> mImgPaths;
    private LayoutInflater mInflater;

    /**
     * 构造方法
     *
     * @param context
     * @param mDatas
     * @param dirPath
     */
    public ImageAdapter(Context context, List<String> mDatas, String dirPath)

    {
        this.mDirPath = dirPath;
        this.mImgPaths = mDatas;
        mInflater = LayoutInflater.from(context);


    }

    @Override
    public int getCount() {
        return mImgPaths.size();
    }

    @Override
    public Object getItem(int position) {
        return mImgPaths.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.grideview_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.mImg = (ImageView) convertView.findViewById(R.id.id_item_image);
            viewHolder.mSelect = (ImageButton) convertView.findViewById(R.id.id_item_select);
            convertView.setTag(viewHolder);

        } else {

            viewHolder = (ViewHolder) convertView.getTag();
        }
           //viewHolder.mImg.setImageResource(R.drawable.p);
         viewHolder.mImg.setColorFilter(null);
          viewHolder.mSelect.setImageResource(R.mipmap.picture_unselected);
       final String filePath = mDirPath + "/" + mImgPaths.get(position);
        /**
         * 图片添加点击事件
         */
        viewHolder.mImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //已经被选择
                if (mSelectImag.contains(filePath)) {
                    mSelectImag.remove(filePath);
                } else {  //没有被选折
                    mSelectImag.add(filePath);
                }
                notifyDataSetChanged();
            }
        });

        if(mSelectImag.contains(filePath))
        {
            viewHolder.mImg.setColorFilter(Color.parseColor("#77000000"));
            viewHolder.mSelect.setImageResource(R.mipmap.pictures_selected);
        }
        ImageLoader.getInstance(3, ImageLoader.Type.LIFO).loadImage(mDirPath + "/" + mImgPaths.get(position), viewHolder.mImg);
        return convertView;
    }


    private class ViewHolder {
        ImageView mImg;
        ImageButton mSelect;

    }


}
